import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttermodule/utils/screen.dart';

import 'UniversityCourseWidget.dart';

class UniversityCourse extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _UniversityCourseState();
  }
}

class _UniversityCourseState extends State<UniversityCourse> {
  @override
  Widget build(BuildContext context) {
    setScreenSize(context);
    return Container(
        child: Flex(
      direction: Axis.vertical,
      children: <Widget>[_searchHead(), _scrollContent()],
    ));
  }
}

//搜索的头部
Widget _searchHead() {
  return Flex(
    direction: Axis.horizontal,
    children: <Widget>[
      Container(
        margin: EdgeInsets.only(
            top: dpHeight(20),
            bottom: dpHeight(20),
            left: dpWidth(15),
            right: dpWidth(33)),
        child: Image.asset('assets/university/images/home_img_back.png'),
        height: dpHeight(48),
        width: dpHeight(48),
      ),
      Expanded(
          flex: 1,
          child: Container(
            padding: EdgeInsets.only(top: dpHeight(15), bottom: dpHeight(15)),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  "assets/university/images/home_img_sousuo.png",
                  height: dpHeight(26),
                  width: dpWidth(26),
                ),
                Padding(
                  padding:
                      EdgeInsets.only(bottom: dpHeight(2), left: dpWidth(12)),
                  child: Text(
                    "课程/讲师",
                    style: TextStyle(
                        fontSize: spFont(28),
                        color: Color.fromARGB(255, 153, 153, 153)),
                  ),
                )
              ],
            ),
            decoration: BoxDecoration(
                color: Color.fromARGB(255, 245, 245, 245),
                borderRadius: BorderRadius.all(Radius.circular(dpHeight(50)))),
          )),
      Container(
        margin: EdgeInsets.only(
            top: dpHeight(20), bottom: dpHeight(20), left: dpWidth(45)),
        child: Image.asset('assets/university/images/home_img_lishi@2x.png'),
        height: dpHeight(48),
        width: dpHeight(48),
      ),
      Container(
        margin: EdgeInsets.only(
            top: dpHeight(20),
            bottom: dpHeight(20),
            left: dpWidth(42),
            right: dpWidth(25)),
        child: Image.asset('assets/university/images/home_img_fankui@2x.png'),
        height: dpHeight(48),
        width: dpHeight(48),
      ),
    ],
  );
}

//滚动的内容
Widget _scrollContent() {
  return Expanded(
      child: Container(
        color: Color.fromARGB(255, 245, 245, 245),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              _banner(),
              _assort(),
              _leaderBoard(),
              _title("为你推荐", true),
              _recommend(),
              _title("微视频", true),
              _microVideo(),
              _title("最新课程", false),
              _newCourse()
            ],
          ),
        ),
      ),
      flex: 1);
}

//banner 位置
Widget _banner() {
  return Container(
    height: dpHeight(300),
    color: Colors.blue,
  );
}

//分类
Widget _assort() {
  return Container(
    margin: EdgeInsets.only(
        top: dpHeight(20), left: dpWidth(20), right: dpWidth(20)),
    child: GridView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: 4,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, childAspectRatio: 2.45),
        itemBuilder: (BuildContext context, int position) {
          return AssortItem();
        }),
    decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(dpHeight(8))),
        boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.031))],
        color: Colors.white),
  );
}

//排行榜
Widget _leaderBoard() {
  return Flex(
    direction: Axis.horizontal,
    children: <Widget>[
      Expanded(
          child: Container(
        margin: EdgeInsets.only(
            top: dpHeight(20), left: dpWidth(20), right: dpWidth(20)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                  right: dpWidth(18), top: dpHeight(27), bottom: dpHeight(27)),
              child: Image.asset(
                'assets/university/images/home_img_xuexibang@2x.png',
                height: dpHeight(34),
                width: dpWidth(34),
              ),
            ),
            Text.rich(TextSpan(children: [
              TextSpan(
                  text: '学习·',
                  style: TextStyle(
                      fontSize: spFont(26),
                      fontWeight: FontWeight.bold,
                      color: Color.fromARGB(255, 51, 51, 51))),
              TextSpan(
                  text: 'TOP榜',
                  style: TextStyle(
                      fontSize: spFont(26),
                      color: Color.fromARGB(255, 51, 51, 51)))
            ]))
          ],
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(dpHeight(8))),
            boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.031))],
            color: Colors.white),
      )),
      Expanded(
          child: Container(
        margin: EdgeInsets.only(top: dpHeight(20), right: dpWidth(20)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(
                  right: dpWidth(18), top: dpHeight(27), bottom: dpHeight(27)),
              child: Image.asset(
                'assets/university/images/home_img_kechengbang@2x.png',
                height: dpHeight(34),
                width: dpWidth(34),
              ),
            ),
            Text.rich(TextSpan(children: [
              TextSpan(
                  text: '课程·',
                  style: TextStyle(
                      fontSize: spFont(26),
                      fontWeight: FontWeight.bold,
                      color: Color.fromARGB(255, 51, 51, 51))),
              TextSpan(
                  text: 'TOP榜',
                  style: TextStyle(
                      fontSize: spFont(26),
                      color: Color.fromARGB(255, 51, 51, 51)))
            ]))
          ],
        ),
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(dpHeight(8))),
            boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.031))],
            color: Colors.white),
      ))
    ],
  );
}

//标题
Widget _title(String title, bool showMore) {
  return Flex(
    direction: Axis.horizontal,
    children: <Widget>[
      Expanded(
          child: Container(
        margin: EdgeInsets.only(
            left: dpWidth(43), top: dpHeight(20), bottom: dpHeight(20)),
        child: Text(
          title,
          style: TextStyle(
              fontWeight: FontWeight.bold,
              color: Color.fromARGB(255, 51, 51, 51),
              fontSize: spFont(36)),
        ),
      )),
      Offstage(
        offstage: !showMore,
        child: Row(
          children: <Widget>[
            Text(
              "查看更多",
              style: TextStyle(
                  color: Color.fromARGB(255, 153, 153, 153),
                  fontSize: spFont(26)),
            ),
            Container(
              margin: EdgeInsets.only(left: dpWidth(12), right: dpWidth(40)),
              child: Image.asset(
                'assets/university/images/home_img_more@2x.png',
                width: dpWidth(25),
                height: dpHeight(25),
              ),
            )
          ],
        ),
      )
    ],
  );
}

//为你推荐
Widget _recommend() {
  return Container(
    margin: EdgeInsets.only(
        top: dpHeight(7), left: dpWidth(20), right: dpWidth(20)),
    child: Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(
              top: dpHeight(24), left: dpWidth(24), right: dpWidth(24)),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(dpHeight(5)),
            child: Image.network(
              "http://fastdfs.dggvip.net:8080/group1/M00/0A/8F/CgAAB122l_WEIdX0AAAAAB27Mbk758.jpg",
            ),
          ),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(dpWidth(2)))),
        ),
        Container(
          margin: EdgeInsets.only(
              top: dpHeight(24),
              left: dpWidth(24),
              right: dpWidth(24),
              bottom: dpHeight(24)),
          child:ClipRRect(
            borderRadius: BorderRadius.circular(dpHeight(5)),
            child:  Image.network(
              "http://fastdfs.dggvip.net:8080/group1/M00/0A/8F/CgAAB122l-eEQaECAAAAAHxlIE4151.jpg",
            ),
          ),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(dpWidth(2)))),
        )
      ],
    ),
    decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(dpHeight(8))),
        boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.031))],
        color: Colors.white),
  );
}

//微视频
Widget _microVideo() {
  return Container(
    padding: EdgeInsets.only(left: dpWidth(10), right: dpWidth(10)),
    margin: EdgeInsets.only(
        top: dpHeight(7), left: dpWidth(20), right: dpWidth(20)),
    child: Column(
      children: <Widget>[
        GridView.builder(
            physics: NeverScrollableScrollPhysics(),
            shrinkWrap: true,
            itemCount: 4,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2, childAspectRatio: 0.95),
            itemBuilder: (BuildContext context, int position) {
              return MicroVideoItem();
            }),
        Container(
          margin: EdgeInsets.only(top: dpHeight(20), bottom: dpHeight(32)),
          child: OutlineButton.icon(
            onPressed: () {},
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20.0)),
            borderSide: BorderSide(color: Color.fromARGB(255, 211, 177, 127)),
            label: Text(
              '换一批',
              style: TextStyle(
                color: Color.fromARGB(255, 211, 177, 127),
              ),
            ),
            icon: ImageIcon(
              AssetImage('assets/university/images/home_img_huanyipi@2x.png'),
              color: Color.fromARGB(255, 211, 177, 127),
              size: dpWidth(26),
            ),
          ),
        )
      ],
    ),
    decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(dpHeight(8))),
        boxShadow: [BoxShadow(color: Color.fromRGBO(0, 0, 0, 0.031))],
        color: Colors.white),
  );
}

//最新课程
Widget _newCourse() {
  return Container(
    color: Colors.white,
    child: ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: 5,
        itemBuilder: (BuildContext context, int position) {
          return NewCourseItem();
        }),
  );
}
